package com.jade.journal.impl;

import javax.ejb.TransactionAttribute;
import javax.inject.Named;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

import com.jade.framework.base.criterion.Criteria;
import com.jade.framework.base.criterion.sql.SqlUtils;
import com.jade.framework.base.util.PagingList;
import com.jade.journal.JournalEntity;
import com.jade.journal.JournalEntityManager;
import org.springframework.transaction.annotation.Transactional;

/**
 * 采用JPA方式实现的JournalEntityManager.
 *
 * @author sky
 * @version 1.0 14-9-23 下午1:47
 */
@Named ("system_journalEntityManager")
public class JournalEntityManagerImpl implements JournalEntityManager
{
    @PersistenceContext (unitName = "default")
    protected EntityManager entityManager;

    @TransactionAttribute
    @Transactional (rollbackFor = Exception.class)
    public void addEntity(JournalEntity entity)
    {
        entityManager.persist(entity);
    }

    public <T extends JournalEntity> PagingList<T> queryEntity(Class entityClass, Criteria criteria)
    {
        String sql = "SELECT journal FROM " + entityClass.getName() + " journal";
        return SqlUtils.sqlQuery(sql, entityManager, criteria);
    }
}
